package com.ruoyi.personnel.oa.sequence.service;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.personnel.oa.sequence.domain.ApproveSequence;
import com.ruoyi.personnel.oa.sequence.domain.ApproveSequenceCond;
import com.ruoyi.personnel.oa.sequence.mapper.ApproveSequenceMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 审批顺序
 * 业务处理层
 */
@Service
public class ApproveSequenceServiceImpl implements IApproveSequenceService{
    @Autowired
    private ApproveSequenceMapper approveSequenceMapper;
    @Autowired
    private SysRoleMapper roleMapper;

    /**
     * 根据搜索条件所有审批顺序记录
     * @param cond 搜索条件
     * @return
     */
    @Override
    public List<ApproveSequence> selectSequenceList(ApproveSequenceCond cond) {
        return approveSequenceMapper.selectSequenceList(cond);
    }

    @Override
    public ApproveSequence selectSequenceById(String approveType) {
        return approveSequenceMapper.selectSequenceById(approveType);
    }

    /**
     * 修改审批顺序
     * @param approveSequence
     * @return
     */
    @Override
    public int updateSequence(ApproveSequence approveSequence) {
        long[] approverArray = StringChangeToArray(approveSequence.getApproverList()); //将字符串转长整型数组
        String[] stringArray = new String[approverArray.length]; // 创建一个字符类型数组存审批人名称列表
        for (int i = 0; i < approverArray.length; i++) {
            SysRole sysRole = roleMapper.selectRoleById(approverArray[i]); // 根据角色ID查询角色信息
            stringArray[i] = sysRole.getRoleName();
        }
        String str = StringUtils.join(stringArray, ","); //数组转字符串
        approveSequence.setApproverNameList(str);
        return approveSequenceMapper.updateSequence(approveSequence);
    }

//    /**
//     * 检查当前登录用户角色中是否在该事件的审批人列表中
//     * @param approveType
//     * @return
//     */
//    @Override
//    public boolean checkLoginRoleIfInApproverList(String approveType) {
//        //获取当前登录用户的角色信息
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        SysUser user = loginUser.getUser();
////        System.out.println("user" + user);
////        Long[] roleIdArray = user.getRoleIds();
//        List<Integer> list = roleService.selectRoleListByUserId(user.getUserId());
//        Long[] roleIdArray = new Long[list.size()];
//        for (int i = 0; i < roleIdArray.length; i++) {
//            roleIdArray[i] = Long.valueOf(list.get(i));
//        }
//        // 获取该事件的审批人列表
//        ApproveSequence approveSequence = approveSequenceMapper.selectSequenceById(approveType);
//        long[] approverArray = StringChangeToArray(approveSequence.getApproverList());
//        for (int i = 0; i < roleIdArray.length; i++) {
//            if (checkRoleIdIfInApproverList(roleIdArray[i], approverArray)) {
//                return true;
//            }
//        }
//        return false;
//    }

//    /**
//     * 找出当前角色在审批人列表中的角色
//     * @param approveType
//     * @return
//     */
//    @Override
//    public long findRoleInApproverList(String approveType) {
//        //获取当前登录用户的角色信息
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        SysUser user = loginUser.getUser();
////        Long[] roleIdArray = user.getRoleIds();
//        List<Integer> list = roleService.selectRoleListByUserId(user.getUserId());
//        Long[] roleIdArray = new Long[list.size()];
//        for (int i = 0; i < roleIdArray.length; i++) {
//            roleIdArray[i] = Long.valueOf(list.get(i));
//        }
//        // 获取该事件的审批人列表
//        ApproveSequence approveSequence = approveSequenceMapper.selectSequenceById(approveType);
//        long[] approverArray = StringChangeToArray(approveSequence.getApproverList());
//        Set<Long> approverSet = new HashSet<>(); // 用一个set集合存放审批人列表
//        for (int i = 0; i < approverArray.length; i++) {
//            approverSet.add(approverArray[i]);
//        }
//        for (int i = 0; i < roleIdArray.length; i++) {
//            if (approverSet.contains(roleIdArray[i])) {
//                return roleIdArray[i];
//            }
//        }
//        return 0;
//    }

    /**
     * 获取最低一级角色
     * @param approveType
     * @return
     */
    @Override
    public long getLowestRoleId(String approveType) {
        // 获取该事件的审批人列表
        ApproveSequence approveSequence = approveSequenceMapper.selectSequenceById(approveType);
//        System.out.println("审批顺序对象" + approveSequence);
        long[] approverArray = StringChangeToArray(approveSequence.getApproverList());
//        System.out.println("数组" + Arrays.toString(approverArray));
        return approverArray[0];
    }

    /**
     * 获取最高一级角色
     * @param approveType
     * @return
     */
    @Override
    public long getHighestRoleId(String approveType) {
        ApproveSequence approveSequence = approveSequenceMapper.selectSequenceById(approveType);
        long[] approverArray = StringChangeToArray(approveSequence.getApproverList());
        return approverArray[approverArray.length - 1];
    }

//    /**
//     * 检查当前登陆角色是否是该事件审批人的最高级
//     * @param approveType
//     * @return
//     */
//    @Override
//    public boolean checkLoginRoleIfIsHighest(String approveType) {
//        //获取当前登录用户的角色信息
//        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        SysUser user = loginUser.getUser();
////        Long[] roleIdArray = user.getRoleIds();
//        List<Integer> list = roleService.selectRoleListByUserId(user.getUserId());
//        Long[] roleIdArray = new Long[list.size()];
//        for (int i = 0; i < roleIdArray.length; i++) {
//            roleIdArray[i] = Long.valueOf(list.get(i));
//        }
//        // 获取该事件的审批人列表
//        ApproveSequence approveSequence = approveSequenceMapper.selectSequenceById(approveType);
//        long[] approverArray = StringChangeToArray(approveSequence.getApproverList());
//        for (int i = 0; i < roleIdArray.length; i++) {
//            if (roleIdArray[i] == approverArray[approverArray.length - 1]) {
//                // 该用户的角色有最高级审批权限
//                return true;
//            }
//        }
//        return false;
//    }

    /**
     * 检查当前角色是否属于审批人列表
     * @param roleId
     * @param approverList
     * @return
     */
    public static boolean checkRoleIdIfInApproverList(long roleId, long[] approverList) {
        for (int i = 0; i < approverList.length; i++) {
            if (approverList[i] == roleId) {
                return true;
            }
        }
        return false;
    }

    /**
     * 字符串转长整型数组
     * @param str
     * @return
     */
    public static long[] StringChangeToArray(String str) {
        String[] stringArray = str.split(","); // 将字符串隔开，转换为字符串数组
        long[] longArray = new long[stringArray.length];
        // 字符串数组转换为整型数组
        for (int i = 0; i < stringArray.length; i++) {
            longArray[i] = Long.parseLong(stringArray[i]);
        }
//        System.out.println(Arrays.toString(longArray));
        return longArray;
    }
}
